package com.schoolMall.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author:zyh
 * @Time:2021-09-04-15:07
 * @email:1269231889@qq.com
 */
public class MD5 {

    public static final String getMd5(String password) {
        char[] hexDigits = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e','f','g'};
        byte[] tmp = password.getBytes();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(tmp);
            byte[] digest = md5.digest();
            int j = digest.length;
            char[] res = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte b = digest[i];
                res[k++] = hexDigits[b >>> 4 & 0xf];
                res[k++] = hexDigits[b & 0xf];
            }
            return new String(res);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
}
